package com.android.androidbigwork.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.android.androidbigwork.R;
import com.android.androidbigwork.entity.Item_Share;
import com.android.androidbigwork.wedget.CircleImageView;
import com.bumptech.glide.Glide;

import java.util.List;

public class Demo_List_Adapter extends ArrayAdapter<Item_Share>{

    private String baUrl = "http://39.108.183.17:8080/images/";

    private Context mContext;

    private Fragment fragment;

    private boolean flag = true;

    private Integer[] resLayoutId;



    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
       Item_Share shares = getItem(position);
        View v;
        final ViewHolder viewHolder;
        if(convertView==null){
            if(flag) {
                v = LayoutInflater.from(getContext()).inflate(R.layout.demo_item, parent,false);
                viewHolder = new ViewHolder();
                viewHolder.user_photo = v.findViewById(R.id.item_user_header_photo);
                viewHolder.name = v.findViewById(R.id.item_user_name);
                viewHolder.message = v.findViewById(R.id.item_message);
                viewHolder.item_gridView = v.findViewById(R.id.item_grid_layout);

                v.setTag(viewHolder);
            }
            else {
//                Log.d("", "getView: "+resLayoutId[0]);
                    v = LayoutInflater.from(getContext()).inflate(resLayoutId[0], parent,false);
                    viewHolder = new ViewHolder();
                    viewHolder.user_photo = v.findViewById(resLayoutId[1]);
                    viewHolder.name = v.findViewById(resLayoutId[2]);
                    viewHolder.message = v.findViewById(resLayoutId[3]);
                    viewHolder.item_gridView = v.findViewById(resLayoutId[4]);

                    v.setTag(viewHolder);

            }

        }
        else {
            v= convertView;
            viewHolder = (ViewHolder)v.getTag();
        }
//        if(viewHolder.name==null)
//            Log.d("", "getView: viewHolder.name==null");
        viewHolder.name.setText(shares.getName());
        viewHolder.message.setText(shares.getMessage());
        viewHolder.item_gridView.setAdapter(initGridAdapter(shares));
        Log.d("", "getView: "+shares.getHeaderPhoto());
        Glide.with(fragment.getContext())
                .load(baUrl+shares.getHeaderPhoto())
                .into(viewHolder.user_photo);
//        Log.d("", "getView: ListView适配器");
        return v;
    }


private class ViewHolder {
    CircleImageView user_photo;
    TextView name;
    TextView message;
    GridView item_gridView;
}


    private GridAdapter initGridAdapter(Item_Share share){
        GridAdapter gridAdapter = new GridAdapter(mContext,null);
        gridAdapter.setFlag(false);
//        List<String>
        gridAdapter.setFragment(fragment);
        gridAdapter.setImgList(share.getImgList());
        gridAdapter.notifyDataSetChanged();
        return gridAdapter;
    }


    public Demo_List_Adapter(@NonNull Context context,
                             int resource,
                             @NonNull List<Item_Share> objects,
                              Fragment fragment) {
        super(context, resource, objects);
        this.mContext = context;
        this.fragment = fragment;
    }


    public void setFlag(boolean f){
        flag=f;
    }
    public void setResLayoutId(Integer[] res){
        this.resLayoutId = res;
    }
}
